package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GAlign;
import g4p_controls.GButton;
import g4p_controls.GCheckbox;
import g4p_controls.GClip;
import g4p_controls.GControlMode;
import g4p_controls.GCustomSlider;
import g4p_controls.GDropList;
import g4p_controls.GEvent;
import g4p_controls.GKnob;
import g4p_controls.GLabel;
import g4p_controls.GOption;
import g4p_controls.GSlider;
import g4p_controls.GToggleControl;
import g4p_controls.GToggleGroup;
import g4p_controls.GValueControl;
import processing.core.PApplet;

public class CustomSlider_Test extends PApplet {

	GCustomSlider sdr;
	int bgcol = 128;
	
	public void setup(){
		size(500,360);
		G4P.setCursor(CROSS);
		sdr = new GCustomSlider(this, 55, 70, 200, 120, "red_yellow18px");
		makeSliderConfigControls();
	}

	public void draw(){
		background(bgcol);
		fill(227,230,255);
		noStroke();
		rect(width - 190, 0, 200, height);
		rect(0, height - 84 , width - 190, 84);
	}

	public void handleSliderEvents(GValueControl slider, GEvent event) { 
		if(slider == sdr)	// The slider being configured?
			println(sdr.getValueS() + "    " + event);		
		if(slider == sdrEasing)
			sdr.setEasing(slider.getValueF());		
		else if(slider == sdrNbrTicks)
			sdr.setNbrTicks(slider.getValueI());		
		else if(slider == sdrBack)
			bgcol = slider.getValueI();	
	}

	public void handleKnobEvents(GValueControl knob, GEvent event) { 
		if(knbAngle == knob)
			this.sdr.setRotation(knbAngle.getValueF(), GControlMode.CENTER);
	}

	public void handleButtonEvents(GButton button, GEvent event) { 
		if(button.tagNo >= 1000)
			sdr.setLocalColorScheme(button.tagNo - 1000);
		else if(btnMakeCode == button)
			placeCodeOnClipboard();
	}
	
	public void handleToggleControlEvents(GToggleControl option, GEvent event) {
		if(option == optLeft)
			sdr.setTextOrientation(G4P.ORIENT_LEFT);
		else if(option == optRight)
			sdr.setTextOrientation(G4P.ORIENT_RIGHT);
		else if(option == optTrack)
			sdr.setTextOrientation(G4P.ORIENT_TRACK);
		else if(option == cbxOpaque)
			sdr.setOpaque(option.isSelected());
		else if(option == cbxValue)
			sdr.setShowValue(option.isSelected());
		else if(option == cbxShowTicks)
			sdr.setShowTicks(option.isSelected());
		else if(option == cbxLimits)
			sdr.setShowLimits(option.isSelected());
		else if(option == cbxSticky)
			sdr.setStickToTicks(option.isSelected());
	}

	public void handleDropListEvents(GDropList list, GEvent event) { 
		if(list == dplStyle)
			sdr.setStyle(dplStyle.getSelectedText());
	}


	private void placeCodeOnClipboard() {
		StringBuilder s = new StringBuilder();
		s.append("// Generated by the GKnob example program\n\n");
		s.append("import g4p_controls.*;\n\n");
		s.append("GCustomSlider sdr; \n\n");
		s.append("void setup() { \n");
		s.append("  size(300, 300); \n");
		s.append("  sdr = new GCustomSlider(this, 55, 70, 200, 120, \"" + dplStyle.getSelectedText() + "\"); \n");
		s.append("  // Some of the following statements are not actually\n");
		s.append("  // required because they are setting the default value. \n");
		s.append("  sdr.setLocalColorScheme(" + sdr.getLocalColorScheme() + "); \n");
		s.append("  sdr.setOpaque(" + sdr.isOpaque() + "); \n");
		s.append("  sdr.setValue(" + sdr.getValueF() + "); \n");
		s.append("  sdr.setNbrTicks(" + sdr.getNbrTicks() + "); \n");
		s.append("  sdr.setShowLimits(" + sdr.isShowLimits() + "); \n");
		s.append("  sdr.setShowValue(" + sdr.isShowValue() + "); \n");
		s.append("  sdr.setShowTicks(" + sdr.isShowTicks() + "); \n");
		s.append("  sdr.setStickToTicks(" + sdr.isStickToTicks() + "); \n");
		s.append("  sdr.setEasing(" + sdr.getEasing() + "); \n");
		s.append("  sdr.setRotation(" + knbAngle.getValueF() + ", PApplet.CENTER); \n");
		s.append("}   \n\n");
		s.append("void draw(){ \n");
		s.append("  background(" + bgcol + "); \n");
		s.append("} \n");
		if(GClip.copy(s.toString()))
			println("Paste code into empty Processing sketch");
		else
			System.err.println("UNABLE TO ACCESS CLIPBOARD");
	}

	// =================================================================================
	// ===================            New pde Tab             ==========================

	GToggleGroup tg = new GToggleGroup();
	GOption optLeft, optRight, optTrack;
	GCheckbox cbxOpaque, cbxShowTicks, cbxValue, cbxLimits, cbxSticky;
	GSlider sdrBack, sdrNbrTicks, sdrEasing;
	GLabel lblOrient, lblVisible, lblNbrTicks, lblEasing, lblSkin;
	GKnob knbAngle;
	GButton btnMakeCode;
	GButton[] btnColours = new GButton[8];
	GDropList dplStyle;
	
	public void makeSliderConfigControls(){
		// Create colour scheme selectors
		int x = width - 42, y = 2;
		for(int i = 0; i < btnColours.length; i++){
			btnColours[i] = new GButton(this, x, y + i * 20, 40, 18, "" + (i+1));
			btnColours[i].tag = "Button: " + (i+1);
			btnColours[i].setLocalColorScheme(i);
			btnColours[i].tagNo = 1000+i;
		}	
		// Create sliders
		x = width-100; y = 162;
		bgcol = 200; 
		sdrBack = new GSlider(this, x, y, 162, 80, 12);
		sdrBack.setLimits(bgcol, 0, 255);
		sdrBack.setRotation(-PI/2);
		sdrBack.setTextOrientation(G4P.ORIENT_RIGHT);
		sdrBack.setEasing(20);
		sdrBack.setShowValue(true);
		sdrBack.setShowTicks(true);
		
		x = width - 180; y = 250;
		
		sdrEasing = new GSlider(this, x, y + 34, 80, 40, 12);
		sdrEasing.setLimits(1.0f, 1.0f, 30.0f);
		sdrEasing.setShowValue(true);
		lblEasing = new GLabel(this, x + 82, y + 34, 80, 40, "Easing");
		lblEasing.setTextAlign(GAlign.LEFT, null);
		lblEasing.setTextItalic();
		
		sdrNbrTicks = new GSlider(this, x, y + 68, 80, 40, 12);
		sdrNbrTicks.setLimits(2, 2, 15);
		sdrNbrTicks.setShowValue(true);
		lblNbrTicks = new GLabel(this, x + 82, y + 68, 80, 40, "No. of ticks");
		lblNbrTicks.setTextAlign(GAlign.LEFT, null);
		lblNbrTicks.setTextItalic();
		
		x = width - 180; y = 164;
		lblOrient = new GLabel(this, x, y, 80, 18, "Orient Text");
		lblOrient.setTextAlign(GAlign.LEFT, null);
		lblOrient.setTextBold();
		optLeft = new GOption(this, x, y + 20, 80, 18, "Left");
		optRight = new GOption(this, x, y + 40, 80, 18, "Right");
		optTrack = new GOption(this, x, y + 60, 80, 18, "Track");
		tg.addControls(optLeft, optRight, optTrack);
		optTrack.setSelected(true);

		x = width - 94; y = 164;
		lblSkin = new GLabel(this, x, y, 80, 18, "Skin");
		lblSkin.setTextBold();
		String[] items = new String[]{ 
			"blue18px","green_red20px","grey_blue","purple18px","red_yellow18px"
		};
		dplStyle = new GDropList(this, x, y + 20, 90, 96, 5);
		dplStyle.setItems(items, 0);
		
		
		x = width - 180; y = 2;
		lblVisible = new GLabel(this, x, y, 70, 18, "Visible");
		lblVisible.setTextBold();
		cbxShowTicks = new GCheckbox(this, x, y + 20, 70, 18, "Ticks");
		cbxLimits = new GCheckbox(this, x, y + 40, 70, 18, "Limits");
		cbxValue = new GCheckbox(this, x, y + 60, 70, 18, "Value");
		cbxOpaque = new GCheckbox(this, x, y + 80 , 70, 18, "Opaque");
		cbxSticky = new GCheckbox(this, x, y + 110, 70, 40, "Stick to ticks");
		
		x = 10; y = height - 80;
		knbAngle = new GKnob(this, x, y, 70, 70, 0.6f);
		knbAngle.setTurnRange(0, 360);
		knbAngle.setLimits(0.0f, 0.0f, TWO_PI);
		knbAngle.setTurnMode(G4P.CTRL_ANGULAR);
		knbAngle.setIncludeOverBezel(true);
		knbAngle.setNbrTicks(13);
		knbAngle.setStickToTicks(true);
		
		btnMakeCode = new GButton(this, x + 100, y + 20, 182, 38, "Place code for existing configuration on clipboard");
	}

}
